//
//  KnowledgeViewController.m
//  Menu
//
//  Created by YouLu on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "KnowledgeViewController.h"

@interface KnowledgeViewController ()

@end

@implementation KnowledgeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.frame =CGRectMake(0, 64+260*HEIGHT+40*HEIGHT, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = YES;
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
}
-(void)loadData:(NSString *)vegetableId
{
    NSString *stepStrUrl = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=4&phonetype=0&is_traditional=0",vegetableId];
    [AFNTool get:stepStrUrl Body:nil HttpResult:JSON success:^(id result) {
        
        NSMutableArray *dataArray = [result objectForKey:@"data"];
        self.knowModelArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in dataArray) {
            KnowModel *knowModel = [[KnowModel alloc] initWithDic:dic];
            [self.knowModelArray addObject:knowModel];
            
        }
        [self.tableView reloadData];
        [self.HUD hide:YES];
    } failure:^(NSError *error) {
        
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:1];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    KnowledgeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[KnowledgeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.knowModel = [self.knowModelArray objectAtIndex:indexPath.row];
    
    
    return cell;
}
//-------------------字体高度----------------
+(CGFloat)heightForcell:(NSString *)content
{
    //参数1.设置计算内容的宽和高
    //参数2.设置计算高度模式
    //参数3.设置计算字体大小属性
    //参数4.系统备用参数,设置为nil
    //其中宽度一定与显示内容的label宽度一致否则计算不准确  高度尽量大一些
    CGSize size=CGSizeMake(295*WIDTH, 10000);
    //字体大小一定与显示内容的label字体大小一致  默认是17.0
    NSDictionary *dic =[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17.0] forKey:NSFontAttributeName];
    
    CGRect frame=  [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KnowModel *knowModel = [self.knowModelArray objectAtIndex:indexPath.row];
    CGFloat knowHeight = [[self class] heightForcell:knowModel.nutritionAnalysis];
    CGFloat makeHeight = [[self class] heightForcell:knowModel.productionDirection];
    return 260*HEIGHT+knowHeight+makeHeight;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
